import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class PersonDAO {
	
	private Connection connection;

	public PersonDAO(Connection connection){
		this.connection=connection;
	}
	
	
	public Persoana save(Persoana persoana){
		try {
			String insertQuery="INSERT INTO date(nume,varsta,stare) values('$nume',$varsta,'$stare')";
			insertQuery=insertQuery.replace("$nume",persoana.getNume());
			insertQuery=insertQuery.replace("$varsta", persoana.getVarsta()+"");
			insertQuery=insertQuery.replace("$stare", persoana.getStare());
			Statement insertStatement=connection.createStatement();
		    int result=insertStatement.executeUpdate(insertQuery);
		    if (result==1){
		    	ResultSet set=insertStatement.getGeneratedKeys();
		    	if (set.next())
		    		persoana.setId(set.getInt(1));
		    	    System.out.println("Salvat persoana "+persoana.getId());
		    	return persoana;
		    }
		    else
		    {
		    	return null;
		    }
			
		} catch (SQLException e) {

			e.printStackTrace();
			return null;
		}
		
		
	}

}
